package com.senmo.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.senmo.adapter.FixedViewPagerAdapter;
import com.senmo.base.ActionBar;
import com.senmo.base.BaseActivity;
import com.senmo.senmo.R;
import com.senmo.video.view.GLVideoView;

import java.util.ArrayList;

import lx.af.adapter.AbsListAdapter;
import lx.af.utils.NetStateUtils;
import lx.af.utils.ViewInject.ViewInject;
import lx.af.view.ProgressWheel;
import com.viewpagerindicator.PageIndicator;

/**
 * author: lx
 * date: 16-1-13
 */
public class ChannelVideoActivity extends BaseActivity implements
        ActionBar.Default.Callback {

    @ViewInject(id = R.id.channel_video_surface)
    private GLVideoView mVideoSurface;
    @ViewInject(id = R.id.channel_video_loading)
    private ProgressWheel mVideoLoadingView;
    @ViewInject(id = R.id.channel_video_pager_indicator)
    private PageIndicator mPagerIndicator;
    @ViewInject(id = R.id.channel_video_function_pager)
    private ViewPager mFunctionPager;
    private TextView mTitle;

    private String mVideoUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mVideoUrl = getIntent().getStringExtra("channel_video_url");
        if (TextUtils.isEmpty(mVideoUrl)) {
            toastShort("channel url empty");
            finish();
            return;
        }
        setContentView(R.layout.activity_channel_video);
        initFunctionPager();
        startVideoPlay();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopVideoPlay();
    }

    @Override
    public void onActionBarCreated(View actionBar, ImageView left, TextView title, ImageView right) {
        // TODO: set channel name as title
        mTitle = title;
    }

    private void startVideoPlay(){
        if (mVideoSurface == null) {
            return;
        }
        if (!NetStateUtils.isNetConnected()) {
            return;
        }
        mVideoLoadingView.setVisibility(View.VISIBLE);
        mVideoSurface.stopPlay();
        mVideoSurface.startPlay(mVideoUrl);
    }

    private void stopVideoPlay() {
        if (mVideoSurface != null) {
            mVideoSurface.stopPlay();
        }
    }

    private void initFunctionPager() {
        ArrayList<View> list = new ArrayList<>();
        list.add(generatePageFuncButtons());
        list.add(generatePageCameraOperate());
        mFunctionPager.setAdapter(new FixedViewPagerAdapter(list));
        mPagerIndicator.setViewPager(mFunctionPager);
    }

    private View generatePageFuncButtons() {
        View view = View.inflate(this, R.layout.video_func_pager_1, null);
        GridView grid = (GridView) view.findViewById(R.id.video_func_pager_1_grid);
        grid.setAdapter(new FuncGridAdapter(this));
        return view;
    }

    private View generatePageCameraOperate() {
        return View.inflate(this, R.layout.video_func_pager_2, null);
    }

    private static class FuncGridAdapter extends AbsListAdapter<String> {

        static ArrayList<String> mIcons = new ArrayList<>(6);
        static {
            mIcons.add("{fa-camera}");
            mIcons.add("{fa-video-camera}");
            mIcons.add("{fa-tv}");
            mIcons.add("{fa-volume-up}");
            mIcons.add("{fa-microphone}");
            mIcons.add("{fa-image}");
        }

        public FuncGridAdapter(Context context) {
            super(context, mIcons);
        }

        @Override
        public View getView(Context context, int position, View convertView, ViewGroup parent) {
            View view = View.inflate(context, R.layout.item_video_func_btn, null);
            TextView tv = (TextView) view.findViewById(R.id.item_video_func_btn);
            tv.setText(getItem(position));
            return view;
        }
    }

}
